import i18n from '@/i18n'
// translate router.meta.title, be used in breadcrumb sidebar tagsview
export function generateTitle(title) {
  const hasKey = this.$te('route.' + title)

  if (hasKey) {
    // $t :this method from vue-i18n, inject in @/lang/index.js
    const translatedTitle = this.$t('route.' + title)

    return translatedTitle
  }
  return title
}
// 处理字符串里的中英文
export function handleServiceI18n(str) {
  if (str) {
    str = str.replace(/m {FACE}/g, 'b {FACE}')
    let strByReplace = str
    let startIndex = 0
    for (const i in str) {
      if (str[i] === '{') {
        startIndex = i
      }
      if (startIndex && str[i] === '}') {
        const alarmText = str.slice(Number(startIndex) + 1, Number(i))
        if (alarmText) {
          const reg = new RegExp(`{${alarmText}}`, 'g')
          i18n.t(alarmText) && (strByReplace = strByReplace.replace(reg, i18n.t(alarmText)))
        }
        startIndex = 0
      }
    }
    return strByReplace
      .replace(/,/g, '，')
      // .replace(/\s*/g, '')
      .replace(/OTDR_NET_FAULT/g, i18n.t('OTDR_NET_FAULT')) // otdr通信异常
      .replace(/TIME_OTDR/g, i18n.t('AFSTASKPAGE_TIME_OTDR')) // 进行例测
      .replace(/TRANSF_LOS/g, i18n.t('ALARM_TYPE_TRANSF_LOS')) // 传输los告警
      .replace(/DEVICE_OFFLINE/g, i18n.t('ALARM_TYPE_DEVICE_OFFLINE')) // 设备离线
      .replace(/MANIPULATOR_FAULT/g, i18n.t('ALARM_TYPE_MANIPULATOR_FAULT')) // AFS机械臂故障
      .replace(/END_LENGTH/g, i18n.t('ALARM_TYPE_END_LENGTH')) // 光纤末端长度告警
      .replace(/LINK_LOSS/g, i18n.t('ALARM_TYPE_LINK_LOSS')) // 链路损耗告警
      .replace(/AVG_LOSS/g, i18n.t('ALARM_TYPE_AVG_LOSS')) // 平均损耗告警
      .replace(/PLUG_LOSS/g, i18n.t('ALARM_TYPE_PLUG_LOSS')) // 接头损耗告警
      .replace(/OTDR_MANUAL_TEST/g, i18n.t('MANUAL_TEST')) // otdr手动测试
  } else {
    return ''
  }
}
